package controller;

import bean.Account;
import bean.Detail;
import dao.DetailDao;
import dao.impl.DetailDaoImpl;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.Initializable;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.stage.Stage;
import util.FxmlUtil;

import java.net.URL;
import java.util.ArrayList;
import java.util.ResourceBundle;

public class DetailController implements Initializable {
    public TableView table;
    public TableColumn fromNo;
    public TableColumn operate;
    public TableColumn toNo;
    public TableColumn toUser;
    public TableColumn money;
    public TableColumn date;
    private DetailDao detailDao = new DetailDaoImpl();

    public void cancelAction() {
        Stage stage = (Stage)this.table.getScene().getWindow();
        stage.setScene(FxmlUtil.SCENE.get("main"));
        stage.setTitle("ATM - 首页");
    }

    public void logoutAction() {
        this.table.getScene().getWindow().hide();
    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        Account account = (Account) FxmlUtil.DATA.get("account");
        ArrayList<Detail> details = detailDao.selectByFromNo(account.getAccNo());

        fromNo.setCellValueFactory(new PropertyValueFactory("fromNo"));
        operate.setCellValueFactory(new PropertyValueFactory("operate"));
        toNo.setCellValueFactory(new PropertyValueFactory("toNo"));
        toUser.setCellValueFactory(new PropertyValueFactory("toUser"));
        money.setCellValueFactory(new PropertyValueFactory("money"));
        date.setCellValueFactory(new PropertyValueFactory("date"));

        ObservableList<Detail> list = FXCollections.observableArrayList(details);
        table.setItems(list);
    }
}
